<?php
/**--------------------
 * Date: 2018/4/27
 * FileName:Code.php 发送验证码
 * Author: 刘远波
 * ProjectName:zhongyi
 * -------------------- */


namespace app\api\controller;


use app\api\ApiBase;
use app\common\Dysms;
use think\Db;

class Code extends ApiBase
{
    /**
     * 短信发送验证码
     *
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function phone()
    {
        $phone = input('phone');
        $type  = input('type');
        if (!$phone) {
            $this->json_error('请输入手机号');
        } else if (!$type) {
            $this->json_error('请输入类型');
        }
        $end_time = time() - 60;
        $table    = Db::name('sms_log');
        $logs     = $table->where([
                                      'log_mobile' => $phone,
                                      'log_type'   => $type,
                                      'user_id'    => $this->user_info['id'],
                                      'add_time'   => [
                                          '>',
                                          $end_time
                                      ],
                                  ])
                          ->find();
        if ($logs) {
            $this->json_success('ok', "短信发送频繁,请稍后再试");
        }
        Db::startTrans();
        $code = getRandNum(6);
        $table->insert([
                           'log_mobile'  => $phone,
                           'log_captcha' => $code,
                           'log_ip'      => request()->ip(),
                           'log_msg'     => '',
                           'log_type'    => $type,
                           'add_time'    => time(),
                           'user_id'     => $this->user_info['id'],
                           'user_name'   => $this->user_info['display_name'],
                           'auth_status' => 0
                       ]);
        $sms      = new Dysms();
        $is_phone = $sms->isPhoneNum($phone);
        if ($is_phone) {
            $result = $sms->sendMsg($phone, $code);
            if (isset($result['Code']) && $result['Code'] == 'OK') {
                Db::commit();
                $this->json_success("ok", '短信发送成功');
            }
            Db::rollback();
            $this->json_error("短信发送失败", $result);
        }
        $this->json_error("手机号码不正确");
    }

    /**
     * 绑定手机号
     *
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function bindPhone()
    {
        $phone = input('phone');
        $code  = input('code');
        $type  = input('type');
        if (!$phone) {
            $this->json_error('请输入手机号');
        } else if (!$type) {
            $this->json_error('请输入类型');
        } else if (!$code) {
            $this->json_error('请输入验证码');
        } else {
            $code_expire = config('code_expire');
            $end_time    = time() - $code_expire;
            $table       = Db::name('sms_log');
            $logs        = $table->where([
                                             'log_mobile'  => $phone,
                                             'log_type'    => $type,
                                             'user_id'     => $this->user_info['id'],
                                             'log_captcha' => $code,
                                             'add_time'    => [
                                                 '>',
                                                 $end_time
                                             ],
                                         ])
                                 ->find();
            if (!$logs) {
                $this->json_error('验证码/手机号码不正确');
            }
            $table->where('log_id', $logs['log_id'])
                  ->update(['auth_status' => 1]);
            $res = Db::name('user')
                     ->where('id', $this->user_info['id'])
                     ->update(['mobile' => $logs['log_mobile']]);
            if ($res) {
                $this->json_success('ok', "绑定手机号成功");
            }
            $this->json_error("绑定手机号失败");
        }
    }
}